#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class GradeProvider : CY.GFive.Core.Providers.DALProvider.IGradeProvider
	{
		#region Sql
		private static readonly string SqlSelectGrade = "SELECT [Id], [GradeCode],[Des],[Prefess] FROM [Grade] WHERE  [Id]=@Id";

		private static readonly string SqlInsertGrade = "INSERT INTO [Grade]([GradeCode],[Des],[Prefess]) values(@GradeCode,@Des,@Prefess);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateGrade = "UPDATE [Grade] SET [GradeCode] = @GradeCode,[Des] = @Des,[Prefess] = @Prefess WHERE  [Id]=@Id";

		private static readonly string SqlDeleteGrade = "DELETE [Grade] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllGrade = "SELECT [Id], [GradeCode],[Des],[Prefess] FROM [Grade]";
		#endregion

		#region IGradeProvider Members
		public Core.Business.Grade Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectGrade);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.Grade grade = new Core.Business.Grade();

				if (!reader.IsDBNull(0)) grade.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) grade.GradeCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) grade.Des = reader.GetString(2);
				if (!reader.IsDBNull(3)) grade.Prefess = reader.GetString(3);

				return grade;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.Grade grade)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@GradeCode", SqlDbType.NVarChar, grade.GradeCode);
			sql.AddParameter("@Des", SqlDbType.NVarChar, grade.Des);
			sql.AddParameter("@Prefess", SqlDbType.NVarChar, grade.Prefess);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertGrade);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) grade.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.Grade grade)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@GradeCode", SqlDbType.NVarChar, grade.GradeCode);
			sql.AddParameter("@Des", SqlDbType.NVarChar, grade.Des);
			sql.AddParameter("@Prefess", SqlDbType.NVarChar, grade.Prefess);
			sql.AddParameter("@Id", SqlDbType.Int, grade.Id);
			sql.ExecuteSql(SqlUpdateGrade);
		}

		public void Delete(Core.Business.Grade grade)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, grade.Id);
			sql.ExecuteSql(SqlDeleteGrade);
		}

		public IList<Core.Business.Grade> GetAllGrade()
		{
			IList<Core.Business.Grade> gradelist = new List<Core.Business.Grade>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllGrade);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.Grade grade = new Core.Business.Grade();

					if (!reader.IsDBNull(0)) grade.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) grade.GradeCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) grade.Des = reader.GetString(2);
					if (!reader.IsDBNull(3)) grade.Prefess = reader.GetString(3);

					grade.MarkOld();
					gradelist.Add(grade);
				}
				reader.Close();
			}
			return gradelist;
		}
		#endregion
	}
}
